package GUIs;

import Configs.Config;
import Controllers.LoaiPhongTroController;
import Controllers.PhongTroController;
import models.PhongTro;

public class f_nhanvien_chunhatro_phongtro_sua extends javax.swing.JFrame {

    private static f_nhanvien_chunhatro_phongtro_sua _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private PhongTroController phongTroControl;
    private LoaiPhongTroController loaiPhongTroControl;
    public PhongTro phongTroSua;
    private String msnv;

    public f_nhanvien_chunhatro_phongtro_sua(Config conn) {
        this.conn = conn;
        initComponents();
    }

    public static f_nhanvien_chunhatro_phongtro_sua getInstance(Config conn, String msnv, String soGPDKKD, String maPhong) {
        if (_instance == null) {
            _instance = new f_nhanvien_chunhatro_phongtro_sua(conn);
        }
        _instance.msnv = msnv;
        _instance.phongTroControl = new PhongTroController(_instance.conn);
        _instance.loaiPhongTroControl = new LoaiPhongTroController(_instance.conn);
        _instance.loaiPhongTroControl.setCbbLoaiPhongWithGPDKKD(_instance.cbxloaiphong, soGPDKKD);
        _instance.phongTroSua = _instance.phongTroControl.getNhaTroWithMaPhong(maPhong);
        _instance.resetForm();
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnthemphongtro = new javax.swing.JPanel();
        lbtieude = new javax.swing.JLabel();
        lbmaphong = new javax.swing.JLabel();
        lbtinhtrang = new javax.swing.JLabel();
        lbloaiphong = new javax.swing.JLabel();
        btnsua = new javax.swing.JButton();
        btnlammoi = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        tfmaphong = new javax.swing.JTextField();
        cbxloaiphong = new javax.swing.JComboBox();
        lbtinhtrang_tt = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sửa thông tin phòng trọ chủ nhà trọ");
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        lbtieude.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbtieude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbtieude.setText("Sửa phòng trọ");

        lbmaphong.setText("Mã phòng:");

        lbtinhtrang.setText("Tình trạng:");

        lbloaiphong.setText("Loại phòng:");

        btnsua.setText("Sửa");
        btnsua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsuaActionPerformed(evt);
            }
        });

        btnlammoi.setText("Làm mới");
        btnlammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlammoiActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        tfmaphong.setEditable(false);

        cbxloaiphong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", "Loại phòng 1", "Loại phòng 2" }));

        lbtinhtrang_tt.setText("Trống");

        javax.swing.GroupLayout pnthemphongtroLayout = new javax.swing.GroupLayout(pnthemphongtro);
        pnthemphongtro.setLayout(pnthemphongtroLayout);
        pnthemphongtroLayout.setHorizontalGroup(
            pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbtieude, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnthemphongtroLayout.createSequentialGroup()
                        .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbmaphong)
                            .addComponent(lbtinhtrang)
                            .addComponent(lbloaiphong))
                        .addGap(37, 37, 37)
                        .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tfmaphong)
                                .addComponent(cbxloaiphong, javax.swing.GroupLayout.Alignment.TRAILING, 0, 211, Short.MAX_VALUE))
                            .addComponent(lbtinhtrang_tt, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(81, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnthemphongtroLayout.createSequentialGroup()
                        .addComponent(btnsua, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnlammoi, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45))))
        );
        pnthemphongtroLayout.setVerticalGroup(
            pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemphongtroLayout.createSequentialGroup()
                .addComponent(lbtieude, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfmaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbmaphong))
                .addGap(16, 16, 16)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbtinhtrang)
                    .addComponent(lbtinhtrang_tt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbxloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbloaiphong))
                .addGap(18, 18, 18)
                .addGroup(pnthemphongtroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnsua)
                    .addComponent(btnlammoi)
                    .addComponent(btnthoat))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnthemphongtro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnthemphongtro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void btnlammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlammoiActionPerformed
        this.resetForm();
    }//GEN-LAST:event_btnlammoiActionPerformed

    private void btnsuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsuaActionPerformed
        if (this.phongTroControl.EditBasic(this)) {
            f_nhanvien_chunhatro_qlnhatro qlnhatro = f_nhanvien_chunhatro_qlnhatro.getInstance(this.conn, this.msnv, this.phongTroSua.getNhatro().getChunhatro().getCMND());
            qlnhatro.refreshdsPhongTro();
            qlnhatro.requestFocus();
            this.dispose();
        }
    }//GEN-LAST:event_btnsuaActionPerformed

    private void resetForm() {
        tfmaphong.setText(this.phongTroSua.getMaphong());
        lbtinhtrang_tt.setText(this.phongTroSua.getTinhtrang() ? "Còn trống" : "Đang thuê");
        cbxloaiphong.setSelectedItem(this.phongTroSua.getLoaiphong().getMotaloai());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlammoi;
    private javax.swing.JButton btnsua;
    private javax.swing.JButton btnthoat;
    public javax.swing.JComboBox cbxloaiphong;
    private javax.swing.JLabel lbloaiphong;
    private javax.swing.JLabel lbmaphong;
    private javax.swing.JLabel lbtieude;
    private javax.swing.JLabel lbtinhtrang;
    private javax.swing.JLabel lbtinhtrang_tt;
    private javax.swing.JPanel pnthemphongtro;
    public javax.swing.JTextField tfmaphong;
    // End of variables declaration//GEN-END:variables
}
